package com.cindata

import grails.gorm.transactions.Transactional

@Transactional
class TimingTaskService
{
    ComponentService componentService
    RiskWarningConfigService riskWarningConfigService

    def execute(String duration)
    {
        def result
        def timingTaskList
        if (duration == 'Everyday')
        {
            timingTaskList = TimingTask.findAll("from TimingTask where duration is null or duration = '' or duration = '${duration}' order by id asc")
        }
        else
        {
            timingTaskList = TimingTask.findAll("from TimingTask where duration = '${duration}' order by id asc")
        }
        timingTaskList?.each {
            result = componentService.evaluate it?.component, null
            it.logs = result
            if (!it?.start)
            {
                it.start = 1
            }
            else
            {
                it.start += 1
            }
            it.save flush: true
        }
    }

    def executeRiskWarning(String duration)
    {
        def riskWarningConfigList = RiskWarningConfig.findAll("from RiskWarningConfig where status = 1 order by id asc")
        println riskWarningConfigList
        riskWarningConfigList?.each 
        {
            def sourceClassType = it?.sourceClassType
            if (sourceClassType == '押品' && it?.duration == duration)
            {
                riskWarningConfigService.executeCollateral(it)
            }
            if (sourceClassType == '贷款' && it?.duration == duration) 
            {
                riskWarningConfigService.executeMortgage(it)
            }
            if (sourceClassType == '借款人' && it?.duration == duration) 
            {
                riskWarningConfigService.executeContact(it)
            }
            if (sourceClassType == '小区' && it?.duration == duration) 
            {
                riskWarningConfigService.executeCommunity(it)
            }
            if (sourceClassType == '机构' && it?.duration == duration) 
            {
                riskWarningConfigService.executeAccount(it)
            }
        }
    }
}
